package com.company.admin.service.people;

import com.company.admin.common.myException.BizException;
import com.company.admin.common.util.StrAndListUtil;
import com.company.admin.dao.file.FileDao;
import com.company.admin.dao.people.ProfessionalPeopleDao;
import com.company.admin.domain.people.ProfessionalPeopleDomain;
import com.company.admin.entity.People.ProfessionalPeopleEntity;
import com.company.admin.entity.file.AttachFileEntity;
import com.company.admin.req.people.ProfessionalPeopleAddRequest;
import com.company.admin.req.people.ProfessionalPeopleDeleteRequest;
import com.company.admin.req.people.ProfessionalPeopleListRequest;
import com.company.admin.req.people.ProfessionalPeopleModifyRequest;
import com.company.admin.resp.people.ProfessionalPeopleListResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 赵成林
 * @date 2019/2/17 19:46
 */
@Service
public class ProfessionalPeopleService {

    @Autowired
    private ProfessionalPeopleDao professionalPeopleDao;
    @Autowired
    private FileDao fileDao;

    /**
     * 暂时没有校验重复
     * 等以后再添加吧，看看有没有身份证号啥的唯一性
     * @param request
     * @return
     */
    @Transactional
    public String add(ProfessionalPeopleAddRequest request){
        checkAdd(request);
        List<Long> pictureIds = request.getPictureIds();
        String picIds = StrAndListUtil.listToStr(pictureIds);
        ProfessionalPeopleEntity entity = new ProfessionalPeopleEntity();
        entity.setCreateTime(new Date());
        entity.setDegree(request.getDegree());
        entity.setMajor(request.getMajor());
        entity.setName(request.getName());
        entity.setPictureIds(picIds);
        entity.setSchool(request.getSchool());
        entity.setUpdateTime( new Date());
        professionalPeopleDao.insert(entity);
        return "success";
    }

    protected void checkAdd(ProfessionalPeopleAddRequest request){
        if (StringUtils.isBlank(request.getName())){
            throw new BizException(204, "名字不可以为空");
        }
    }

    @Transactional
    public String modify(ProfessionalPeopleModifyRequest request){

        checkModify(request);
        List<Long> pictureIds = request.getPictureIds();
        String picIds = StrAndListUtil.listToStr(pictureIds);
        ProfessionalPeopleEntity entity = new ProfessionalPeopleEntity();
//        entity.setCreateTime(new Date());
        entity.setDegree(request.getDegree());
        entity.setMajor(request.getMajor());
        entity.setName(request.getName());
        entity.setPictureIds(picIds);
        entity.setSchool(request.getSchool());
        entity.setUpdateTime( new Date());
        entity.setProfessionanPeopleId(request.getProfessionanPeopleId());
        professionalPeopleDao.modify(entity);
        return "success";
    }

    protected void checkModify(ProfessionalPeopleModifyRequest request){
        if (request.getProfessionanPeopleId() == null){
            throw new BizException(204, "ProfessionanPeopleId 为空");
        }
        ProfessionalPeopleEntity entity = professionalPeopleDao.getById(request.getProfessionanPeopleId());
        if (entity == null){
            throw new BizException(204, "不存在的people, id是 ： " + request.getProfessionanPeopleId());
        }
    }

    public String delete(ProfessionalPeopleDeleteRequest request){
        checkDelete(request);
        professionalPeopleDao.deleteByIds(request.getProfessionanPeopleIds());
        return "success";
    }
    protected void checkDelete(ProfessionalPeopleDeleteRequest request){
        if (CollectionUtils.isEmpty(request.getProfessionanPeopleIds())){
            throw new BizException(204, "请选择要删除的people");
        }
    }

    /**
     * 列表
     * @param request
     * @return
     */
    public ProfessionalPeopleListResponse list (ProfessionalPeopleListRequest request){
        if (request.getLength() == null || request.getOffset() == null){
            throw new BizException(204, "分页信息参数不完整");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("offset", request.getOffset());
        map.put("length", request.getLength());
        List<ProfessionalPeopleEntity> entities = professionalPeopleDao.list(map);
        Long totalCount = professionalPeopleDao.totalCount();
        ProfessionalPeopleListResponse response = new ProfessionalPeopleListResponse();
        response.setTotalCount(totalCount);
        List<ProfessionalPeopleDomain> domains = new ArrayList<>();
        for (ProfessionalPeopleEntity entity : entities){
            ProfessionalPeopleDomain domain = new ProfessionalPeopleDomain();
            domain.setCreateTime(entity.getCreateTime());
            domain.setDegree(entity.getDegree());
            domain.setMajor(entity.getMajor());
            domain.setName(entity.getName());
            domain.setProfessionanPeopleId(entity.getProfessionanPeopleId());
            domain.setSchool(entity.getSchool());
            domain.setUpdateTime(entity.getUpdateTime());
            String pictureIds = entity.getPictureIds();
            List<AttachFileEntity> attachFileEntities = new ArrayList<>();
            List<Long> attachFileIds = StrAndListUtil.strToList(pictureIds);
            // 其实不应该在循环里面进行select的骚操作，不过这个项目的数据应该不大，问题不大吧
            if (CollectionUtils.isNotEmpty(attachFileIds)){
                attachFileEntities = fileDao.selectByIds(attachFileIds);
            }
            domain.setAttachFileEntities(attachFileEntities);
            domains.add(domain);
        }

        response.setProfessionalPeopleDomains(domains);
        return response;
    }
}
